package com.huixuebao.insure.hezhong;

import com.huixuebao.common.util.RSASign;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * TODO
 *
 * @author fatao_yu
 * @version 1.0
 * @date 2020-11-04 21:06
 */
@Slf4j
public class CreatePostPackage {

    public static String getMD5Mac(String stSourceString)
    {
        String mystring;
        byte getbyte[];
        try
        {
            getbyte = getMD5Mac(stSourceString.getBytes("utf-8"));
        }
        catch(UnsupportedEncodingException e)
        {
            return null;
        }
        mystring = bintoascii(getbyte);
        return(mystring.toUpperCase());
    }

    public static byte[] getMD5Mac(byte[] bySourceByte)
    {
        byte[] byDisByte;
        MessageDigest md;
        try
        {
            md = MessageDigest.getInstance("MD5");
            md.reset();
            md.update(bySourceByte);
            byDisByte = md.digest();
        }
        catch(NoSuchAlgorithmException n)
        {
            return(null);
        }
        return(byDisByte);
    }

    public static String bintoascii(byte[] bySourceByte)
    {
        int len, i;
        byte tb;
        char high, tmp, low;
        String result = new String();
        len = bySourceByte.length;
        for(i = 0; i < len; i++)
        {
            tb = bySourceByte[i];
            tmp = (char)((tb >>> 4) & 0x000f);
            if(tmp >= 10)
                high = (char)('a' + tmp - 10);
            else
                high = (char)('0' + tmp);
            result += high;
            tmp = (char)(tb & 0x000f);
            if(tmp >= 10)
                low = (char)('a' + tmp - 10);
            else
                low = (char)('0' + tmp);
            result += low;
        }
        return result;
    }

    /**
     * 注意：
     * 1、如果测试合众接口，采用MD5流程
     * 2、如果测试提供给太平的接口，采用SHA流程
     * @param args
     * @throws Exception
     */
   public static void main(String[] args) throws Exception{
        //md5Exec();
       sha1Exec();
    }

    private static void sha1Exec() throws Exception {
        // 待签名数据
        String data = "{\\\"head\\\":{\\\"orderId\\\":\\\"20201123110851001\\\",\\\"productCode\\\":\\\"847685440856284001\\\",\\\"insureType\\\":\\\"proposal\\\",\\\"applyTime\\\":\\\"2020-23-10 11:38:20\\\",\\\"extendInfos\\\":{}},\\\"body\\\":{\\\"insObject\\\":{\\\"insureCount\\\":\\\"1\\\",\\\"totalPremium\\\":\\\"7900\\\",\\\"totalAmount\\\":\\\"790000\\\",\\\"effectiveStartTime\\\":\\\"2020-11-24 00:00:00\\\",\\\"effectiveEndTime\\\":\\\"2021-11-23 23:59:59\\\",\\\"courseId\\\":\\\"000104\\\",\\\"mechanismId\\\":\\\"0024cace2a0f4e6a9ee74b8eb44bb6c4\\\",\\\"province\\\":\\\"上海\\\",\\\"city\\\":\\\"上海\\\",\\\"address\\\":\\\"上海市闵行区虹中路535号308室\\\",\\\"extendInfos\\\":{\\\"educationalName\\\":\\\"上海笔尖文化传播有限公司\\\",\\\"educationalIdType\\\":\\\"统一社会信用代码\\\",\\\"educationalId\\\":\\\"91110108788965958K\\\",\\\"studentType\\\":\\\"1\\\",\\\"studentName\\\":\\\"测试小朋友名称\\\",\\\"studentIdType\\\":\\\"99\\\",\\\"studentIdNo\\\":\\\"20120113\\\",\\\"educationalProject\\\":\\\"成人班\\\",\\\"learningModality\\\":\\\"线下\\\",\\\"educationalAddress\\\":\\\"上海市闵行区东兰路320号牌楼智谷科技园1号楼201\\\",\\\"educationalPayment\\\":\\\"790000\\\",\\\"educationalStartTime\\\":\\\"2020-11-20\\\",\\\"educationalEndTime\\\":\\\"2021-11-19\\\",\\\"educationalAttachType\\\":\\\"2\\\",\\\"educationalContract\\\":[\\\"https://h5.huizhuanqian.net/hxb_union/union/upload/20201028/251c19a93b4d41b0983ecc5997e35ffc.jpg\\\",\\\"\\\"],\\\"educationalSchedule\\\":[\\\"https://h5.huizhuanqian.net/hxb_union/union/upload/20201028/251c19a93b4d41b0983ecc5997e35ffc.jpg\\\",\\\"\\\"],\\\"extendInfos\\\":{}}},\\\"holder\\\":{\\\"holderType\\\":\\\"2\\\",\\\"holderName\\\":\\\"北京恒鑫智盛科技有限公司\\\",\\\"holderPhone\\\":\\\"17701618951\\\",\\\"holderMail\\\":\\\"test@mail.com\\\",\\\"holderIdType\\\":\\\"3\\\",\\\"holderIdNo\\\":\\\"91110108788965958K\\\",\\\"holderIdStartTime\\\":\\\"2020-11-23\\\",\\\"holderIdEndTime\\\":\\\"2021-11-22\\\",\\\"holderNationality\\\":\\\"CHN\\\",\\\"holderProfessional\\\":\\\"R\\\",\\\"extendInfos\\\":{}},\\\"insured\\\":{\\\"insuredRelation\\\":\\\"99\\\",\\\"insuredIdType\\\":\\\"01\\\",\\\"insuredIdNo\\\":\\\"310228197801051484\\\",\\\"insuredIdStartTime\\\":\\\"2020-11-24\\\",\\\"insuredIdEndTime\\\":\\\"2021-11-23\\\",\\\"insuredName\\\":\\\"测试人员\\\",\\\"insuredPhone\\\":\\\"18101889106\\\",\\\"insuredMail\\\":\\\"\\\",\\\"insuredNationality\\\":\\\"\\\",\\\"insuredProfessional\\\":\\\"80000\\\",\\\"extendInfos\\\":{}}}}";

        // 签名密钥
        String signSecretKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIRJJ71l/t6VBrwVKd2ZUozmChOqqOjZNznmSrWUp6l8qFeUATh5cV3XXE0EgCrIhGnrtj/55WBgWmrcTk8x4BZnnoFX2kRYXV3BYjQTKnx1wGGKT/ow+URabym3Nr39saIuUNm9lclntor0NUUVecsvhrndNKOTl+D6FdgFRr5pAgMBAAECgYAb7o5zkWvFd9YaNKXIYq1pjtEl4e0UbXkZwqSXvrHAOdAZ3eh8Ejh7xhN5N7AQWJKNAvZSf+OM/AtbcPUgB7n2810CZoIrhyDbfFSmBp1BB8Fr3qNeG074GcQP7lj70e7Oeh3gACBxRNIhFzc6Ba6HjmYB99dq+9xPhAQ9f6IBAQJBAL4M73Qhk6KRTpWwU91SX/Zv3W8MWfzfoJwuzzIidzW+815SeEw2oi2BjQOSeJbTfVmJDjdT0Ez8an6e24A2xRkCQQCyMLWaYLRi1kKBDONeci8gXYKDsOii22FaM5gnTi1+Tqf8yyvLH9uwGUfqC0y36izjnWQOfMNhHK1FyG5xnB3RAkA7w5OVYG2txKYCuoVU5u+Ybosiva6ar7QxCSNM77uvIhIkCikcRl3vk3rFsczMGUuk2U2oJmotm37dvJToF9EZAkBHBLLcfH5ohgyrcn+MPOYbeCl0YyXVAqqiUL3rrHTlSmQZlMA3jNL6NJk85pQFGQgbD7e5LnCQW+WTUnbyQM6BAkEAm4VeijvLgFUktJFrUKIPHdr9+dkOO+FEE0eKf8PG6KfSdhSZIOID2eLUqMskOMDU5Oth/NW+sbt5cv2wPypxBw==";
        String sign = RSASign.getSha1Sign(data,signSecretKey);
        System.out.println(sign);
        log.info("sign {}", sign);
    }

    private static void md5Exec() {
        // 待签名数据
        String data = "aONd3ydW1Q8O2fT+P2ImLQvV3cp3a2RXy8Ccjh5K9TEEqwP3zs8C9KU+tmxv7Cc595XR8l4pmTaeDL9ErKZzTtsOHHJyZQbjLho1RubsXpTBYWAFmJlVCQmXtwZNW/jP3p/xFvY/xIm1E2PL90roZwBWuebrXcDu/shw5YYQPrZA4pWDp8MI3DBzWKTFz+yv1R6uZ9xKA15LO3eSKD+XRA==";
        // 双方预约好的md5秘钥
        String signSecretKey = "union";

        // 准备待签名字符串
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(data);
        stringBuilder.append(signSecretKey);
        // 计算签名
        String sign = getMD5Mac(stringBuilder.toString());
        log.info("sign {}", sign);
    }


}